package org.sun.sunshine.config;

import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class ParkTableMergeSameColRenderPolicy extends DynamicTableRenderPolicy {

    //heads
    private final List<String> heads;
    //ignoreCols
    private List<Integer> mergeCols;
    private int tableWidth;
    private int[] cellWidth;

    public ParkTableMergeSameColRenderPolicy(List<String> heads, List<Integer> mergeCols, int tableWidth, int[] cellWidth) {
        this.heads = heads;
        this.mergeCols = mergeCols;
        this.tableWidth = tableWidth;
        this.cellWidth = cellWidth;
    }

    @Override
    public void render(XWPFTable table, Object data) throws Exception {
        //添加表头
        XWPFTableRow headRow = table.createRow();
        table.setWidth(tableWidth);
        for(int i=0;i<heads.size();i++){
            XWPFTableCell cell = headRow.createCell();
            cell.setColor("808080");
            //获取 XWPFTableCell 的CTTc
            CTTc ctTc = cell.getCTTc();
            //获取 CTP
            CTP ctP = (ctTc.sizeOfPArray() == 0) ?
                    ctTc.addNewP() : ctTc.getPArray(0);
            //getParagraph(ctP) 获取 XWPFParagraph
            XWPFParagraph par = cell.getParagraph(ctP);
            //XWPFRun   设置格式
            XWPFRun run = par.createRun();
            run.setFontSize(12);
            run.setText(heads.get(i));
            run.setBold(true);
            run.setFontFamily("仿宋_GB2312");
            cell.setWidth(String.valueOf(cellWidth[i]));
        }

        //设置数据   mergeCells(row1, 0, 2)
        List<String[]> dataStr = (List<String[]>) data;
        Map<Integer,Integer[]> mergeColsMap = new HashMap<>();
        for(int i=1;i<dataStr.size();i++){
            //将数据添加到表格，并用mergeColsMap 记录待合并列的起止行
            String[] rowData = dataStr.get(i);
            XWPFTableRow row = table.createRow();
            for(int j=0;j<rowData.length;j++){
                XWPFTableCell cell = row.createCell();
                cell.setWidth(String.valueOf(cellWidth[j]));
                cell.setText(rowData[j]);
                cell.setColor("808080");
                //获取 XWPFTableCell 的CTTc
                CTTc ctTc = cell.getCTTc();
                //获取 CTP
                CTP ctP = (ctTc.sizeOfPArray() == 0) ?
                        ctTc.addNewP() : ctTc.getPArray(0);
                //getParagraph(ctP) 获取 XWPFParagraph
                XWPFParagraph par = cell.getParagraph(ctP);
                //XWPFRun   设置格式
                XWPFRun run = par.createRun();
                run.setFontSize(12);
                run.setText(heads.get(i));
                run.setBold(true);
                run.setFontFamily("仿宋_GB2312");

                //合并
                if(i>1&&mergeCols.contains(j)){
                    Integer[] range = mergeColsMap.get(j);
                    if(range==null){
                        range = new Integer[]{i,i};
                    }else{
                        if(rowData[j].equals(dataStr.get(i-1)[j])){
                            range[1]=i;
                        }
                    }
                }
            }
        }
        //合并单元格
        mergeColsMap.forEach((k,v)->{
            mergeCellsVertically(table,k,v[0],v[1]);
        });
    }


    /**
     * @Description: 跨列合并
     * table要合并单元格的表格
     * row要合并哪一行的单元格
     * fromCell开始合并的单元格
     * toCell合并到哪一个单元格
     */
    public  void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
        for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
            XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
            if ( cellIndex == fromCell ) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

    /**
     * @Description: 跨行合并
     * table要合并单元格的表格
     * col要合并哪一列的单元格
     * fromRow从哪一行开始合并单元格
     * toRow合并到哪一个行
     */
    public  void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
        for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
            XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
            if ( rowIndex == fromRow ) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
        }
    }



}
